<template>
  <div class="kingkongareastyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <!-- 提示 -->
    <p style="color: #969799; font-size: 12px; margin-top: 10px"> 拖动选中的导航可对其排序 </p>

    <!-- 图片导航 -->
    <div v-if="datas.imageList[0]">
      <vuedraggable v-model="datas.imageList" v-bind="dragOptions">
        <transition-group>
          <section v-for="(item, index) in datas.imageList" :key="item + index" class="imgList">
            <i class="el-icon-circle-close" @click="deleteimg(index)" />
            <!-- 图片 -->
            <div class="imag">
              <img draggable="false" @click="showUpload('second', index)" :src="item.src"  alt="" />
            </div>
            <!-- 标题和链接 -->
            <div class="imgText">
              <el-input v-model="item.text" placeholder="请输入标题" size="mini" clearable />
              <div style="height: 5px" />
              <el-input v-model="item.desc" placeholder="请输入描述" size="mini" clearable />
              <div style="height: 5px" />
              <!-- 选择类型 -->
              <select-link @selectThisLink="selectThisLink" @clearThisLink="clearThisLink(index)" :linktypeurl="item.url" :theIndex="index" />
              <div style="height: 5px" />
              <div class="vipshow">
                <span style="font-size:14px;">VIP标识：</span>
                <el-radio-group v-model="item.isVipShow">
                  <el-radio :label="2">隐藏</el-radio>
                  <el-radio :label="1">显示</el-radio>
                </el-radio-group>
              </div>
            </div>
          </section>
        </transition-group>
      </vuedraggable>
    </div>

    <!-- 上传图片 -->
    <el-button class="uploadImg" type="primary" plain @click="showUpload('first')">
      <i class="el-icon-plus" />点击添加图文导航
    </el-button>

    <div class="bor" />

    <!-- 表单 -->
    <el-form label-width="80px" :model="datas" size="small" :rules="rules">
      <!-- 文字大小 -->
      <el-form-item label="文字大小" prop="textSize" :hide-required-asterisk="true" class="lef">
        <el-input v-model.number="datas.textSize" type="number" placeholder="请输入文字大小" :maxlength="2" />
      </el-form-item>

      <div style="height: 10px" />

      <!-- 图片圆角 -->
      <el-form-item label="图片圆角" class="lef borrediu">
        <el-slider v-model="datas.borderRadius" :max="50" input-size="mini" show-input />
      </el-form-item>

      <div style="height: 10px" />

      <!-- 文字颜色 -->
      <el-form-item class="lef" label="文字颜色">
        <!-- 颜色选择器 -->
        <el-color-picker v-model="datas.textColor" show-alpha class="picke" :predefine="predefineColors" />
      </el-form-item>
    </el-form>

    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>

<script>
import uploadimg from "../../uploadImg"; // 图片上传
import vuedraggable from "vuedraggable"; // 拖拽组件
import SelectLink from "../../selectLink";
export default {
  name: "Kingkongareastyle",
  components: { 
    uploadimg, 
    vuedraggable, 
    SelectLink 
  },
  props: {
    datas: Object,
  },
  data() {
    const textSize = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入有效数字"));
      if (value > 40) {
        callback(new Error("数字不能超过40"));
        this.datas.textSize = '12';
      } else {
        this.datas.textSize = value;
      }
    };
    return {
      dragOptions: {
        // 拖拽配置
        animation: 200,
      },
      rules: {
        textSize: [{ required: true, validator: textSize, trigger: "blur" }]
      },
      predefineColors: [
        // 颜色选择器预设
        "#ff4500",
        "#ff8c00",
        "#ffd700",
        "#90ee90",
        "#00ced1",
        "#1e90ff",
        "#c71585",
        "#409EFF",
        "#909399",
        "#C0C4CC",
        "rgba(255, 69, 0, 0.68)",
        "rgb(255, 120, 0)",
        "hsv(51, 100, 98)",
        "hsva(120, 40, 94, 0.5)",
        "hsl(181, 100%, 37%)",
        "hsla(209, 100%, 56%, 0.73)",
        "#c7158577",
      ],
      emptyText: "",
      changeType: "",
      changeIndex: ""
    };
  },
  watch: {
    // datas: {
    //   handler: function (params) {
    //     sessionStorage.setItem("checkedIds", JSON.stringify(this.datas.ridArr));
    //   },
    //   immediate: true,
    //   deep: true,
    // },
  },
  created() {},
  methods: {
    showUpload(type, index) {
      this.changeType = type;
      this.changeIndex = index;
      this.$refs.upload.showUpload();
    },
    // 提交
    uploadInformation(res) {
      if (this.changeType == "first") {
        this.datas.imageList.push({
          src: res,
          text: "",
          desc: "",
          id: "",
          isVipShow: 2
        });
      } else {
        this.datas.imageList[this.changeIndex].src = res;
      }
    },
    // 选择链接
    selectThisLink(resu) {
      this.datas.imageList.forEach((it, ind) => {
        if (ind == resu.index) {
          it.url = resu.url;
          it.linktype = resu.linktype;
          it.id = resu.id;
          it.text = resu.name;
          if(resu.note != undefined){
            it.desc = resu.note;
          }
        }
      });
    },
    clearThisLink (index) {
      this.datas.imageList[index].url = '';
    },
    /* 删除图片列表的图片 */
    deleteimg(index) {
      this.datas.imageList.splice(index, 1);
    },
  },
};
</script>

<style scoped lang="scss">
.kingkongareastyle {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 0 10px 20px;
  box-sizing: border-box;
  .bor {
    height: 1px;
    margin: 20px 0;
    background-color: #ebedf0;
  }
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }

  .lef {
    .el-form-item__label {
      text-align: left;
    }
  }

  /* 上传图片按钮 */
  .uploadImg {
    width: 345px;
    height: 40px;
    margin-top: 10px;
  }

  /* 商品列表 */
  .imgList {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;

    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }

    /* 图片 */
    .imag {
      width: 60px;
      height: 60px;
      border-radius: 5px;
      overflow: hidden;
      position: relative;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        display: inline-block;
      }
      span {
        background: rgba(0, 0, 0, 0.5);
        font-size: 12px;
        position: absolute;
        left: 0px;
        bottom: 0px;
        display: inline-block;
        width: 100%;
        text-align: center;
        color: #fff;
        height: 20px;
        line-height: 20px;
      }
    }

    /* 图片字 */
    .imgText {
      width: 80%;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      padding-left: 20px;
      justify-content: space-between;
      .select-type {
        display: flex;
        .el-select {
          .el-input {
            input {
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }
        }
      }
    }
  }

  /* 图片样式 */
  .weiz {
    text-align: right;
    i {
      padding: 5px 14px;
      margin-left: 10px;
      border-radius: 0;
      border: 1px solid #ebedf0;
      font-size: 20px;
      font-weight: 500;
      cursor: pointer;

      &:last-child {
        font-size: 22px;
      }

      &.active {
        color: #155bd4;
        border: 1px solid #155bd4;
        background: #e0edff;
      }
    }
  }

  .shop-head-pic {
    color: #ababab;
    display: flex;
    flex-direction: column;
    .home-bg {
      width: 100px;
      height: 100px;
      margin: 10px auto;
    }
    .shop-head-pic-btn {
      display: flex;
      flex-direction: row;
      .el-button {
        flex: 1;
      }
    }
  }
  /* 颜色选择器 */
  .picke {
    float: right;
  }
}
</style>
